package com.tshopping.dao;

import com.tshopping.bean.Address;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;

@Repository
public interface AddressDao {

    @Insert("insert into address (name,phone,address,user,province,city,district) values (#{name},#{phone},#{address},#{user},#{province},#{city},#{district})")
    boolean insertAddress(@Param("name") String name, @Param("phone") String phone, @Param("address") String address, @Param("user") int user, @Param("province") String province, @Param("city") String city, @Param("district") String district);

    @Select("select * from address where user = #{user}")
    ArrayList<Address> selectUserOfAddressesByUserId(@Param("user") int user);

    @Update("update address set name = #{name},phone = #{phone},address = #{address},province = #{province},city = #{city},district = #{district} where id = #{id}")
    boolean updateAddressByAddressId(@Param("name") String name, @Param("phone") String phone, @Param("address") String address, @Param("province") String province, @Param("city") String city, @Param("district") String district, @Param("id") int id);

    @Update("update address set isDefault = 1 where id = #{newId}")
    boolean updateAddressDefaultByAddressId(@Param("newId") int newId);

    @Update("update address set isDefault = 0 where id=#{oldId}")
    boolean updateCancelDefaultByAddressId(@Param("oldId") int oldId);

    @Delete("delete from address where id = #{id}")
    boolean deleteAddressByAddressId(@Param("id") int id);

    @Select("select * from address where user=#{user} and isDefault = 1")
    Address selectDefaultAddressByUser(@Param("user") int user);

    @Select("select * from address where id=#{id}")
    Address selectAddressById(@Param("id")int id);

}
